package com.tiantian.manager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.html5.HTML5Activity;
import com.html5.PlayApplication;
import com.html5.R;
import com.music.model.Music;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.tiantian.service.PlayerService;

public class PlayerPlugin extends Plugin {
	private boolean isPlaying=false;
	private boolean isPause=false;
	private boolean isReleased=false;
	private PlayApplication app=null;
	private Music music=null;
	@Override
	public PluginResult execute(String action, JSONArray param, String callbackId) {
		PluginResult.Status status = PluginResult.Status.OK;
		String result = "";
		try {
			if (action.equals("playMusic")) {
				JSONObject obj= param.getJSONObject(0);
				Music music=new Music();
				music.setName(obj.getString("name"));
				music.setSinger(obj.getString("singer"));
				music.setMusicUrl(obj.getString("musicUrl"));
				showNotification(R.drawable.logo, "天天音乐", "天天音乐", obj.getString("name"));
				playMusic(music);
			}else if (action.equals("pauseMusic")) {
				pauseMusic();
			} else if (action.equals("jump")) {
				JSONObject obj= param.getJSONObject(0);
				Music music=new Music();
				music.setName(obj.getString("name"));
				music.setSinger(obj.getString("singer"));
				music.setMusicUrl(obj.getString("musicUrl"));
				showNotification(R.drawable.logo, "天天音乐", "天天音乐", obj.getString("name"));
				jump(music);
			}else if (action.equals("playInfo")) {
				
				result=getPlayInfo().toString();
			}else {
				status = PluginResult.Status.INVALID_ACTION;
			}
			return new PluginResult(status, result);
		} catch (Exception e) {
			return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
		}
		
	}
	//开始播放
    public void playMusic(Music music){
				Intent intent=new Intent();
				intent.putExtra("music",music);
				intent.putExtra("msg", 0);
				intent.setClass(this.ctx,PlayerService.class);
				this.ctx.startService(intent);
    }
    //暂停播放
    public void pauseMusic(){

    	Intent intent=new Intent();
		intent.putExtra("msg", 1);
		intent.setClass(this.ctx,PlayerService.class);
		this.ctx.startService(intent);
		
    }
  
    //跳歌
    public void jump(Music music){
    	Intent intent=new Intent();
		intent.putExtra("music",music);
		intent.putExtra("msg", 2);
		intent.setClass(this.ctx,PlayerService.class);
		this.ctx.startService(intent);
    	
    }
    public void showNotification(int icon, String tickertext, String title,String content) {
		NotificationManager nm = (NotificationManager) this.ctx.getSystemService(this.ctx.NOTIFICATION_SERVICE);
		// Notification管理器
		Notification notification = new Notification(icon, tickertext,System.currentTimeMillis());
		notification.flags=Notification.FLAG_ONGOING_EVENT;//该标志表示当用户点击 Clear 之后，不能够清除该通知。
		//notification.defaults = Notification.DEFAULT_ALL;
		
		
		Intent intent=new Intent(this.ctx,HTML5Activity.class);
		/**
		 * Intent.FLAG_ACTIVITY_CLEAR_TOP ：如果在当前Task中，有要启动的Activity，那么把该Acitivity之前的所有Activity都关掉，并把此Activity置前以避免创建Activity的实例
           Intent.FLAG_ACTIVITY_NEW_TASK ：系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task，若有，则在该Task上创建Activity，若没有则新建具有该Activity属性的
		 */
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pt = PendingIntent.getActivity(this.ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
        RemoteViews remoteViews=new RemoteViews(this.ctx.getPackageName(),R.layout.notivication);
        remoteViews.setImageViewResource(R.id.logo, R.drawable.logo);
        remoteViews.setTextViewText(R.id.title,title);
        remoteViews.setTextViewText(R.id.info,"正在播放－－"+content);
        
        notification.contentView=remoteViews;
        notification.contentIntent=pt;
		nm.notify(19172439, notification);

	}
    public JSONObject getPlayInfo() {
    	app=(PlayApplication)this.ctx.getApplicationContext();
    	JSONObject obj=new JSONObject();
    	try {
			obj.put("currentPosition", app.getCurrentPosition());
			obj.put("duration", app.getDuration());
			obj.put("isPlaying", app.isPlaying());
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
    	return  obj;
    }


}
